package top.zhimingw.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import top.zhimingw.pojo.Article;
import top.zhimingw.pojo.Comment;
import top.zhimingw.redis.ArticleKey;
import top.zhimingw.redis.CommentKey;
import top.zhimingw.service.BaseService;
import top.zhimingw.service.CommentService;

import java.util.List;

/**
 * @ClassName: CommentServiceImpl
 * @Description:
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/6/30 14:48
 */
@Service("commentService")
public class CommentServiceImpl extends BaseService implements CommentService {

    @Override
    public boolean insert(Comment comment) {
        if(commentMapper.insert(comment)==1){
            return true;
        }
        return false;
    }

    /**
    * @Description: 根据文章ID和类型获取文章
    * @Param: [aId, type]
    * @return: java.util.List<top.zhimingw.pojo.Comment>
    * @Author: zhimingw
    * @Date: 2021/6/30
    */
    @Override
    public List<Comment> queryByArticleIdAndType(Integer aId, Integer type) {
        List<Comment> comments = null;
        if(redisService.exists(CommentKey.getByArticleId,String.valueOf(aId))){
            comments = redisService.getList(CommentKey.getByArticleId,String.valueOf(aId),Comment.class);
        }else{
            comments = commentMapper.queryByArticleIdAndType(aId,type);
            comments.forEach(comment -> {
                List<Comment> list = commentMapper.queryReplyComment(comment.getId());
                comment.setReply(list);
            });
            redisService.setList(CommentKey.getByArticleId,String.valueOf(aId),comments);
        }
        return comments;
    }

    /**
    * @Description: 获取评论的数量
    * @Param: []
    * @return: java.lang.Integer
    * @Author: zhimingw
    * @Date: 2021/7/5
    */
    @Override
    public Integer getCount() {
        return commentMapper.getCount();
    }

    @Override
    public Integer getUnReadCount() {
        return commentMapper.getUnReadCount();
    }

    /**
    * @Description: 实体作为筛选条件查询数据
    * @Param: [comment]
    * @return: java.util.List<top.zhimingw.pojo.Comment>
    * @Author: zhimingw
    * @Date: 2021/7/6
    */
    @Override
    public List<Comment> queryAll(Comment comment) {
        List<Comment> comments = commentMapper.queryAll(comment);
        //获取所有回复
        comments.forEach(temp->{
            Comment search = new Comment();
            search.setReplyId(temp.getId());
            List<Comment> list = commentMapper.queryAll(search);
            temp.setReply(list);
        });
        return comments;
    }

    /**
    * @Description: 查询单条数据
    * @Param: [id]
    * @return: top.zhimingw.pojo.Comment
    * @Author: zhimingw
    * @Date: 2021/7/7
    */
    @Override
    public Comment queryById(Integer id) {
        Comment comment = commentMapper.queryById(id);
        comment.setTitle(articleMapper.getTitle(comment.getArticleId()));
        comment.setReply(commentMapper.queryReplyComment(comment.getId()));
        return comment;
    }


    @Override
    public boolean update(Comment comment) {
        if(commentMapper.update(comment)==1){
            return true;
        }
        return false;
    }

    /**
    * @Description: 通过文章id获取评论数量
    * @Param: [id]
    * @return: java.lang.Integer
    * @Author: zhimingw
    * @Date: 2021/7/7
    */
    @Override
    public Integer queryByArticleId(Integer id) {
        Integer count = 0;
        if(redisService.exists(CommentKey.getByCommentCount,String.valueOf(id))){
            count = redisService.get(CommentKey.getByCommentCount,String.valueOf(id),Integer.class);
        }else{
            count = commentMapper.queryByArticleId(id);
            redisService.set(CommentKey.getByCommentCount,String.valueOf(id),count,0);
        }
        return count;
    }

    /**
    * @Description: 删除Comment
    * @Param: [id]
    * @return: boolean
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public boolean deleteByArticle(Integer id) {
        if(commentMapper.deleteByArticleId(id)>0){
            return true;
        }
        return false;
    }

    /**
    * @Description: 删除Comment
    * @Param: [id]
    * @return: boolean
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public boolean deleteById(Integer id) {
        if(commentMapper.deleteById(id)==1){
            return true;
        }
        return false;
    }

    /**
    * @Description: 删除Comment
    * @Param: [id]
    * @return: boolean
    * @Author: zhimingw
    * @Date: 2021/7/20
    */
    @Override
    public boolean deleteByReplyId(Integer id) {
        if(commentMapper.deleteByReplyId(id)==1){
            return true;
        }
        return false;
    }
}
